const { src, dest, series } = require("gulp")
const fs = require("fs")
const gulpless = require("gulp-less")
const path = require("path")
const cleanCss = require("gulp-clean-css")
const rename = require("gulp-rename")

const dist = path.join(__dirname, "dist")

function write(lujing, content) {
    fs.writeFile(lujing, content, (err) => {
        if (err) {
            throw err
        } else {
            console.log("ok");
        }
    })
}

const lessc = () => {
    return src("src/style/index.less")
        .pipe(gulpless())
        .pipe(dest("dist"))
}

const style = (cb) => {
    fs.readFile("./src/style/index.less", (err, data) => {
        if (err) {
            throw err
        } else {
            if (!fs.existsSync(dist)) {
                fs.mkdirSync(dist)
            }
            write(dist + "/index.less", data.toString())
            fs.readFile(dist + "/index.css", (err, file) => {
                if (err) throw err
                file = file.toString().replace(/\/\*{1,2}[\s\S]*\*\//g, "").replace(/\s+/g, "")
                write(dist + "/index.min.css", file)
            })
        }

    })

    cb()
}

const dev = series(lessc, style)

module.exports = {
    style,
    dev
}